package io.renren.modules.sys.web_api.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.utils.Stringutils;
import io.renren.modules.sys.back.entity.BtCategroyManageEntity;
import io.renren.modules.sys.back.entity.BtCtBaseEntity;
import io.renren.modules.sys.back.entity.BtCtGaishuEntity;
import io.renren.modules.sys.back.entity.BtWendaInfoEntity;
import io.renren.modules.sys.back.service.BtCategroyManageService;
import io.renren.modules.sys.back.service.BtCtBaseService;
import io.renren.modules.sys.back.service.BtCtGaishuService;
import io.renren.modules.sys.back.service.BtWendaInfoService;
import io.renren.modules.sys.web_api.service.ProductPageService;
import io.renren.modules.sys.web_api.service.WendaPageService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("wendaPageService")
public class WendaPageServiceImpl implements WendaPageService{
   @Autowired
   private BtWendaInfoService btWendaInfoService;
    @Override
    public List<Map<String,Object>> getAllWenda(){
        List<Map<String,Object>>  datalist = new ArrayList<>();
        Collection<String> c = new ArrayList();
        c.add("tisj");
        List<BtWendaInfoEntity> ctbeanlist = btWendaInfoService.selectList(new EntityWrapper<BtWendaInfoEntity>().orderDesc(c));
        for(BtWendaInfoEntity sb:ctbeanlist){
            Map<String,Object> data = new HashMap<>();
            data.put("name",sb.getName());
            data.put("link","/api/wenda/detail/"+sb.getId());
            data.put("img",sb.getImg());
            data.put("time",sb.getTisj());
            data.put("llcs",sb.getLlcs());
            String tmp = sb.getSummer();
            if(!tmp.equals("")){
                Document doc = Jsoup.parse(tmp);
                Elements subimg = doc.getElementsByClass("infozyao");
                if(subimg!=null&&subimg.size()>0) {
                    data.put("content",subimg.get(0).childNode(4).outerHtml());
                }
                datalist.add(data);
            }
        }
        return datalist;
    }
    @Override
    public Map<String,Object> getWenDaDatailInfo(Long id){
        Map<String,Object> backdata = new HashMap<>();
        BtWendaInfoEntity rwbean = btWendaInfoService.selectById(id);
        backdata.put("naverji","技术问答");
        backdata.put("naverjilink","/api/wenda/first");
        backdata.put("navwuji",rwbean.getName());
        Map<String,Object> subcontent = null;
        //概述技术
        subcontent = new HashMap<>();
        subcontent.put("name","简介");
        subcontent.put("content",rwbean.getSummer());
        backdata.put("gaishu",subcontent);
        //工作原理
        subcontent = new HashMap<>();

        if(rwbean.getContent()!=null){
            subcontent.put("name","内容");
            subcontent.put("content",rwbean.getContent());
            backdata.put("gongzhuyuanli",subcontent);
        }
        rwbean.setLlcs(rwbean.getLlcs()+1);
        btWendaInfoService.insertOrUpdate(rwbean);
        return backdata;
    }
}
